package com.muki.uri.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用以缓存已经爬取过得uri
 * @author ljh_2017
 *
 */
public class SpiderUriMemoryCache {

	private static final Map<String,List<String>> uris = new HashMap<>();
	
	/**
	 * 将uri推送到指定集合中
	 * @param topic
	 * @param uri
	 */
	public static void put(String topic,String uri) {
		List<String> results = uris.getOrDefault(topic, new ArrayList<String>());
		results.add(uri);
		uris.put(topic, results);
	}

	//判断是否重复
	/**
	 * 判重
	 * @param topic
	 * @param uri
	 * @return
	 */
	public static boolean isRepetition(String topic,String uri) {
		List<String> results = uris.getOrDefault(topic, null);
		if(results!=null && results.contains(uri)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 去重机制
	 * 如果重复则不操作
	 * 否则存入缓存中
	 * @param topic
	 * @param uri
	 */
	public static boolean putIfAbsent(String topic,String uri) {
		if(!isRepetition(topic, uri)) {
			put(topic, uri);
			return true;
		} else
			System.out.println("去掉重复:"+uri);
		return false;
	}
	
	public static List<String> getUrls(String topic) {
		return uris.getOrDefault(topic,null);
	}

	/**
	 * 设置持久化
	 * @param persistence
	 */
	public static void setPersistence(OverUriPersistence persistence,String key,List<String> value) {
		persistence.persistence(key,value);
	}

}
